<?php

session_start();

DEFINE("FRAMEWORK_PATH", dirname( __FILE__ ) ."/" );

require_once( FRAMEWORK_PATH . 'lib/utils.php');
require('registry/registry.class.php');

$registry = new Registry();
// setup our core registry objects
$registry->createAndStoreObject( 'template', 'template' );
$registry->createAndStoreObject( 'mysqldb', 'db' );
$registry->createAndStoreObject( 'authenticate', 'authenticate' );
$registry->createAndStoreObject( 'urlprocessor', 'url' );
$registry->createAndStoreObject( 'mailout', 'mailout' );

$registry->getObject('url')->getURLData();
// database settings
include(FRAMEWORK_PATH . 'config.php');
// create a database connection
$registry->getObject('db')->newConnection( $configs['db_host_sn'], $configs['db_user_sn'], $configs['db_pass_sn'], $configs['db_name_sn']);

$registry->getObject('authenticate')->checkForAuthentication();

// store settings in our registry
$settingsSQL = "SELECT `key`, `value` FROM settings";
$registry->getObject('db')->executeQuery( $settingsSQL );
while( $setting = $registry->getObject('db')->getRows() )
{
	$registry->storeSetting( $setting['value'], $setting['key'] );
}
$registry->getObject('template')->getPage()->addTag( 'siteurl', $registry->getSetting('siteurl') );

$controllers = array();
$controllersSQL = "SELECT * FROM controllers WHERE active=1";
$registry->getObject('db')->executeQuery( $controllersSQL );
while( $controller = $registry->getObject('db')->getRows() )
{
	$controllers[] = $controller['controller'];
}

$registry->createAndStoreObject('FBConnector', 'fbconnector');
$controller = $registry->getObject('url')->getURLBit(0);


if( $registry->getObject('authenticate')->isLoggedIn() )
{	
	$registry->getObject('template')->buildFromTemplates('main/main.tpl.php', 'main/main_menu.tpl.php', 'main/main_content.tpl.php','main/footer.tpl.php');
	
	if( !(in_array( $controller, $controllers ) ))
	$controller = 'profile';

	require_once( FRAMEWORK_PATH . 'controllers/' . $controller . '/controller.php');
	$controllerInc = $controller.'controller';
	$controller = new $controllerInc( $registry, true );
	
	//===============
	// Load online friends
	require_once( FRAMEWORK_PATH . 'models/relationships.php');
	require_once (FRAMEWORK_PATH . 'registry/template.class.php');
	$members = new Relationships( $registry );
	$template = new Template($registry);
	
	$template->buildFromTemplates('main/onlinefriends.tpl.php');

	$content = '';

	$loadLimit = 4;
	$loadedRows = 0;
	$offset = 0;
	$userid = $registry->getObject('authenticate')->getUser()->getUserID();
	$profileid = $registry->getObject('authenticate')->getUser()->getCurrentProfile()->getProfileID();
	$profile = $registry->getObject('authenticate')->getUser()->getCurrentProfile();
	
	$profileTyepe = $profile->getProfileType();
		
	// facebook profile
	/*if ($profileTyepe == 2){
				
		$fb_connector = $profile->getInitFBConnector();			
	}
	*/	

	$cache = $members->getOnlineFriends( $profile, $loadLimit, $offset);

	while ( ($loadedRows < 5 && $members->loadedRows > 0))
	{
		$template->getPage()->addPPTag('ofrow_id', $loadedRows);
		$template->addTemplateBit('onlinemember_row'.$loadedRows, 'main/onliefriends_row.tpl.php');
		$template->getPage()->addPPTag( 'onlinefriends'.$loadedRows, array( 'DATA', $cache ) );
		$template->parseOutput();

		++$loadedRows;
		$next = "{onlinemember_row{ofrow_id}}";
		$template->getPage()->addPPTag('ofnext_row', $next);
		$template->parseOutput();

		$offset+=$members->loadedRows;

		$cache = $members->getOnlineFriends( $profile, $loadLimit, $offset);
	}

	$template->getPage()->addTag('onlinemember_row{ofrow_id}', '');
	

	$template->parseOutput();
	$content = $template->getPage()->getContent();
	//================

	$registry->getObject('template')->getPage()->addTag( 'ONLINE_FRIENDS', $content);

	$cache = $registry->getObject('db')->cacheData(  $registry->getObject('authenticate')->getUser()->getProfiles() );
	$registry->getObject('template')->getPage()->addTag( 'profiles', array( 'DATA', $cache) );
	$registry->getObject('template')->getPage()->addTag('sitename', $registry->getSetting('sitename'));

	$currentProfile = $registry->getObject('authenticate')->getUser()->getCurrentProfile(); //Current logged in users profile

	$registry->getObject('template')->getPage()->addTag( 'current_profile_name', $currentProfile->getName());
	$registry->getObject('template')->getPage()->addPPTag( 'current_profile', $currentProfile->getProfileName());
	$registry->getObject('template')->getPage()->addPPTag( 'current_profile_photo', $currentProfile->getPhoto());
	$registry->getObject('template')->getPage()->addPPTag( 'current_profile_id', $currentProfile->getProfileID());

	if ( (isset($_SESSION['current_view_ProfileID']) && intval($_SESSION['current_view_ProfileID']) > 0)
	&& (isset($_SESSION['current_view_userID']) && intval($_SESSION['current_view_userID']) > 0))
	{
		require_once( FRAMEWORK_PATH . 'models/profile.php' );
		$currentProfile = new Profile( $registry, $_SESSION['current_view_userID'], $_SESSION['current_view_ProfileID']);
	}

	$dob = $currentProfile->getDOB();
	$seconds = strtotime($dob);

	$time = time_stamp($seconds);

	$registry->getObject('template')->addTemplateBit('SIDEBAR1', 'main/main_sidebar1.tpl.php');
	$registry->getObject('template')->addTemplateBit('MAIN_MENU', 'main/main_rightmenu.tpl.php');

	$registry->getObject('template')->getPage()->addTag( 'current_view_profile_age', $time);
	$registry->getObject('template')->getPage()->addTag( 'current_view_profile_name', $currentProfile->getName());
	$registry->getObject('template')->getPage()->addTag( 'current_view_profile', $currentProfile->getProfileName());
	$registry->getObject('template')->getPage()->addTag( 'current_view_profile_photo', $currentProfile->getPhoto());
	$registry->getObject('template')->getPage()->addTag( 'current_view_profile_id', $currentProfile->getProfileID());

	require_once( FRAMEWORK_PATH . 'controllers/notifications/notifications.php');
	$notifications = new Notificatons($registry, $registry->getObject('authenticate')->getUser(),
	$registry->getObject('authenticate')->getUser()->getCurrentProfile());

	$count = $notifications->getNotificationCount();
	if ($count > 0)
	{
		$notificationCount = '<b>'.'['.$count.']'.'</b>';
		$registry->getObject('template')->getPage()->addTag( 'NOTIFICATION_COUNT', $notificationCount);
	}
	else
	$registry->getObject('template')->getPage()->addTag( 'NOTIFICATION_COUNT', '');
}
else
{
	$registry->getObject('template')->buildFromTemplates('login.tpl.php');
	$url = $registry->getObject('url')->buildURL( array('profile', 'home'), '', false );
	$registry->getObject('template')->getPage()->addTag( 'referer', $url);
	$registry->getObject('template')->getPage()->addTag( 'error_message', "");
	$registry->getObject('template')->getPage()->addTag( 'reg_error_message', "");
	$registry->getObject('template')->getPage()->addTag( 'TAB_ID', "loginlink");
	$registry->getObject('template')->getPage()->addTag('sitename', $registry->getSetting('sitename'));

	if ($controller == "authenticate" && in_array( $controller, $controllers ) )
	{

		require_once( FRAMEWORK_PATH . 'controllers/' . $controller . '/controller.php');
		$controllerInc = $controller.'controller';
		$controller = new $controllerInc( $registry, true );

	}
}

$registry->getObject('template')->parseOutput();
echo ($registry->getObject('template')->getPage()->getContentToPrint());

?>

